package com.wulian.common.error;


import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wulian.common.vo.DefaultBody;
import com.wulian.common.vo.ResultMoudel;

import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;

/**
 * 
* @ClassName: RestErrorEndpoint 
* @Description: TODO(统一 /error 返回信息) 
* @author 马志豪
* @date 2019年9月10日 下午3:04:27 
*
 */
@ApiIgnore
public class RestErrorEndpoint implements ErrorController {
    @Override
    public String getErrorPath() {
        return "/error";
    }
    @ApiIgnore
    @RequestMapping(value = "/error")
    public ResponseEntity<ResultMoudel> error(HttpServletRequest request) {

        HttpStatus status = this.getStatus(request);
        ResultMoudel body = new ResultMoudel<>(ResultMoudel.ERROR_FLAG, new DefaultBody(status.getReasonPhrase()));
        return new ResponseEntity<>(body, status);
    }

    protected HttpStatus getStatus(HttpServletRequest request) {
        Integer statusCode = (Integer)request.getAttribute("javax.servlet.error.status_code");
        if (statusCode == null) {
            return HttpStatus.INTERNAL_SERVER_ERROR;
        } else {
            try {
                return HttpStatus.valueOf(statusCode);
            } catch (Exception var4) {
                return HttpStatus.INTERNAL_SERVER_ERROR;
            }
        }
    }
}
